package com.atguigu.dao.impl;

import com.atguigu.dao.FoodDao;
import com.atguigu.pojo.Food;

import java.util.List;

public class FoodDaoImpl extends baseDao<Food> implements FoodDao {
    @Override
    public void addFood(Food food) {
        String sql = "insert into t_food(`food_name`,`price`, `food_id`) values(?,?,?)";
        update(sql, food.getFood_name(), food.getPrice(), food.getFood_id());
    }

    @Override
    public void deleteFoodById(Integer id) {
        String sql = "delete from t_food where id = ?";
        update(sql, id);
    }

    @Override
    public void updateFood(Food food) {
        String sql = "update t_food set `food_name`=?, `price`=?, `food_id`=? where id = ?";
        update(sql, food.getFood_name(), food.getPrice(), food.getFood_id(), food.getId());
    }

    @Override
    public Food queryFoodById(Integer id) {
        String sql = "select `id` , `food_name` , `price` , `img_path` , `food_id` from t_food where id = ?";
        return getInstance(sql, id);
    }

    @Override
    public long getCount() {
        String sql = "select count(*) from t_food";
        return getValue(sql);
    }

    @Override
    public long getClassCount(Integer food_id) {
        String sql = "select count(*) from t_food where food_id = ?";
        return getValue(sql, food_id);
    }

    @Override
    public List<Food> queryFoods() {
        String sql = "select `id` , `food_name` , `price` , `img_path`, `food_id` from t_food";
        return getForList(sql);
    }

    @Override
    public List<Food> queryLimitFoods(Integer page, Integer size) {
        String sql = "select `id` , `food_name` ,  `price` , `img_path` , `food_id` from t_food limit ?, ?";
        return getForListLimit(sql, (page - 1)*size, size);
    }

    @Override
    public List<Food> queryLimitClassFoods(Integer food_id, Integer page, Integer size) {
        String sql = "select `id` , `food_name` , `price` , `img_path` , `food_id` from t_food where food_id=? limit ?, ?";
        return getForListLimit(sql, food_id, (page - 1)*size, size);
    }

    @Override
    public List<Food> queryPriceFoods(Integer min, Integer max, Integer page, Integer size) {
        String sql = "SELECT `id` , `food_name` , `price` , `img_path`, `food_id` FROM t_food WHERE price>=? AND price <=?  LIMIT ?, ?";
        return getForListLimit(sql, min, max, (page - 1)*size, size);
    }

    @Override
    public List<Food> queryPriceCount(Integer min, Integer max) {
        String sql = "SELECT `id` , `food_name` , `price` , `img_path`, `food_id` FROM t_food WHERE price>=? AND price <=?";
        return getForListLimit(sql, min, max);
    }

    @Override
    public List<Food> queryPriceClassFoods(Integer food_id, Integer min, Integer max, Integer page, Integer size) {
        String sql = "SELECT `id` , `food_name` , `price` , `img_path`, `food_id` FROM t_food WHERE price>=? AND price <=? ANd food_id=? LIMIT ?, ?";
        return getForListLimit(sql, min, max, food_id, (page - 1)*size, size);
    }

    @Override
    public List<Food> queryPriceClassCount(Integer food_id, Integer min, Integer max) {
        String sql = "SELECT `id` , `food_name` , `price` , `img_path`, `food_id` FROM t_food WHERE price>=? AND price <=? AND food_id=?";
        return getForListLimit(sql, min, max, food_id);
    }


}

